﻿using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using HtmlAgilityPack;
namespace HtmlPictureHelper.Analyzer
{
    /// <summary>
    /// 分析器基类
    /// </summary>
    public abstract class BaseAnalyzer
    {
        public BaseAnalyzer(string enterLink)
        {
            this.EnterLink = enterLink;
            string htmlString = HttpGet(enterLink);
            this.HtmlDocument = new HtmlDocument();
            this.HtmlDocument.LoadHtml(htmlString);
        }

        public HtmlDocument HtmlDocument { get; set; }
        /// <summary>
        /// 分析器入口链接
        /// </summary>
        public string EnterLink { get; set; }


        protected string GetHtmlNodeInnerText(string expression,Enums.ExpressionType expressionType= Enums.ExpressionType.XPath)
        {
            if (HtmlDocument == null)
            {
                return "";
            }
            HtmlNode node= null;

            if (expressionType == Enums.ExpressionType.XPath)
            {
                node = HtmlDocument.DocumentNode.SelectSingleNode(expression); 
            }
            else if(expressionType == Enums.ExpressionType.Css)
            {
                node = HtmlDocument.DocumentNode.QuerySelector(expression);
            }

            if (node == null || string.IsNullOrEmpty(node.InnerText))
            {
                return "";
            }
            return node.InnerText;
        }
        /// <summary>
        /// 使用Get方式获取网页源代码
        /// </summary>
        /// <param name="url">网页地址</param>
        /// <param name="htmlDocumentEncoding">网页文档编码，可以强制指定</param>
        /// <returns></returns>
        protected string HttpGet(string url,Encoding htmlDocumentEncoding=null)
        {
            string responsestr = "";
            HttpWebRequest req = HttpWebRequest.Create(url) as HttpWebRequest;
            req.Accept = "*/*";
            req.Method = "GET";
            req.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1";
            using (HttpWebResponse response = req.GetResponse() as HttpWebResponse)
            {
                Stream stream;
                if (response.ContentEncoding.ToLower().Contains("gzip"))
                {
                    stream = new GZipStream(response.GetResponseStream(), CompressionMode.Decompress);
                }
                else if (response.ContentEncoding.ToLower().Contains("deflate"))
                {
                    stream = new DeflateStream(response.GetResponseStream(), CompressionMode.Decompress);
                }
                else
                {
                    stream = response.GetResponseStream();
                }
                Encoding encoding = htmlDocumentEncoding;
                if(encoding == null)
                {
                    encoding = Encoding.GetEncoding(response.CharacterSet);
                }
                using (StreamReader reader = new StreamReader(stream, encoding))
                {
                    responsestr = reader.ReadToEnd();
                    stream.Dispose();
                }
            }
            return responsestr;
        }
    }
}
